import * as fetcherLotSize from "../moex/fetch-lot-size"
import * as fetcherPrice from "../moex/fetch-price"
import { getPortfolio } from "./investor"

const data = [
    {
        "code": "AAA",
        "count": 100,
        "freeFloat": 0.55,
        "weight": 0.50
      },
      {
        "code": "BBB",
        "count": 100,
        "freeFloat": 0.48,
        "weight": 0.30
      },
      {
        "code": "CCC",
        "count": 200,
        "freeFloat": 0.5,
        "weight": 0.20
      },
]


describe('IndexInvestor:', () => {
    it('возвращает пустой список активов при нулевом балансе', async () => {
        const fetchLotSizeMock = jest.spyOn(fetcherLotSize, 'fetchLotSize')
        fetchLotSizeMock.mockResolvedValue(1)
        const fetchPriceMock = jest.spyOn(fetcherPrice, 'fetchPrice')
        fetchPriceMock.mockResolvedValue(100)

        const assets = await getPortfolio(data, 0)
        
        expect(assets).toHaveLength(0)
    })

    it('возвращает пустой список активов при нулевом балансе (учитывая lotSize)', async () => {
        const fetchLotSizeMock = jest.spyOn(fetcherLotSize, 'fetchLotSize')
        fetchLotSizeMock.mockResolvedValue(10)
        const fetchPriceMock = jest.spyOn(fetcherPrice, 'fetchPrice')
        fetchPriceMock.mockResolvedValue(100)

        const assets = await getPortfolio(data, 900)
        
        expect(assets).toHaveLength(0)
    })

    it('возвращает список с одним активом', async () => {
        const fetchLotSizeMock = jest.spyOn(fetcherLotSize, 'fetchLotSize')
        fetchLotSizeMock.mockResolvedValue(1)
        const fetchPriceMock = jest.spyOn(fetcherPrice, 'fetchPrice')
        fetchPriceMock.mockResolvedValue(100)

        const assets = await getPortfolio(data, 100)
        
        expect(assets).toEqual([{"code": "AAA", "count":1, lotSize: 1, price: 100, weight: 0.5}])
    })

    it('возвращает список с одним активом (учитывая lotSize)', async () => {
        const fetchLotSizeMock = jest.spyOn(fetcherLotSize, 'fetchLotSize')
        fetchLotSizeMock.mockResolvedValue(10)
        const fetchPriceMock = jest.spyOn(fetcherPrice, 'fetchPrice')
        fetchPriceMock.mockResolvedValue(100)

        const assets = await getPortfolio(data, 1000)
        
        expect(assets).toEqual([{"code": "AAA", "count":10, lotSize: 10, price: 100, weight: 0.5}])
    })

    it('возвращает список со всеми активам', async () => {
        const fetchLotSizeMock = jest.spyOn(fetcherLotSize, 'fetchLotSize')
        fetchLotSizeMock.mockResolvedValue(1)
        const fetchPriceMock = jest.spyOn(fetcherPrice, 'fetchPrice')
        fetchPriceMock.mockResolvedValue(100)

        const assets = await getPortfolio(data, 100000)
        
        expect(assets)
        .toEqual([
            {"code": "AAA", "count":500, lotSize: 1, price: 100, weight: 0.5},
            {"code": "BBB", "count":300, lotSize: 1, price: 100, weight: 0.3},
            {"code": "CCC", "count":200, lotSize: 1, price: 100, weight: 0.2}
        ])
    })
})